package net.zui.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import net.zui.Option;
/**
 * 日历工具类
 * <br /><br />
 * <b>作者:</b>张天文 <br />
 * <b>日期:</b>2017年6月7日  <br />
 * @date
 */
public class UCal implements Comparable<UCal>{
	private Calendar cal;

	public UCal() {
		cal=Calendar.getInstance();
	}

	public UCal(Calendar cal) {
		this.cal = cal;
	}
	
	public UCal(int year,int month,int day) {
		this();
		set(year,month,day,23,59,59);
	}
	
	public UCal(String date,String splitRegx){
		cal=Calendar.getInstance();
		String[] str=date.split(splitRegx);
		int len=str.length;
		if(len<3){
			throw new IndexOutOfBoundsException("日期字符串:"+date+"经过regx:"+splitRegx+"分割数组长度小于3!");
		}else if(len<6 && len>=3){
			set(Integer.valueOf(str[0]),Integer.valueOf(str[1]),Integer.valueOf(str[2]));
		}else{
			set(Integer.valueOf(str[0]),Integer.valueOf(str[1]),Integer.valueOf(str[2]),Integer.valueOf(str[3]),Integer.valueOf(str[4]),Integer.valueOf(str[5]));
		}
	}

	public int year(){
		return cal.get(Calendar.YEAR);
	}
	
	public int month(){
		return cal.get(Calendar.MONTH)+1;
	}
	public int pre_month(){
		return cal.get(Calendar.MONTH);
	}
	public int day(){
		return cal.get(Calendar.DATE);
	}
	
	public int hour(){
		return cal.get(Calendar.HOUR_OF_DAY);
	}
	
	public int minute(){
		return cal.get(Calendar.MINUTE);
	}
	
	public int second(){
		return cal.get(Calendar.SECOND);
	}
	
	public int dayInWeek(){
		int week= cal.get(Calendar.DAY_OF_WEEK);
		return week==1?7:week-1;
	}
	
	public int weekInMonth(){
		return cal.get(Calendar.WEEK_OF_MONTH);
	}
	
	public void set(int year,int month,int day){
		cal.set(year, month-1, day);
	}
	
	public void set(int year,int month,int day,int hour,int minute,int second){
		cal.set(year, month-1, day,hour,minute,second);
	}
	
	public void setTime(int hour,int minute,int second){
		cal.set(year(), month()-1, day(),hour,minute,second);
	}
	
	public void roll(int day){
		cal.roll(Calendar.DATE, day);
	}
	
	public void add(int day){
		cal.add(Calendar.DATE, day);
	}
	
	public void addMonth(int month){
		cal.add(Calendar.MONTH, month);
	}
	
	public int dateDiff(Calendar calt){
		return dateDiff(new UCal(calt));
	}
	
	public int dateDiff(int year,int month,int day){
		Calendar calt=Calendar.getInstance();
		calt.set(year, month, day);
		return dateDiff(calt);
	}
	
	public int dateDiff(UCal util){
		int comp=this.compareTo(util);
		UCal tmp1=this.clone(),tmp2=util.clone();
		if(comp==0){
			return 0;
		}else if(comp>0){
			tmp1.setTime(0, 0, 1);
			tmp2.setTime(23, 59, 59);
		}else{
			tmp2.setTime(0, 0, 1);
			tmp1.setTime(23, 59, 59);
		}
		
		long time=tmp1.cal.getTimeInMillis()-tmp2.cal.getTimeInMillis();
		return (int)Math.ceil(time/(1000*3600*24.0));
	}
	
	public UCal monday(){
		UCal calt=clone();
		int week=dayInWeek();
		if(week==1){
			
		}else{
			calt.add((week-1)*-1);
		}
		return calt;
	}
	
	public int maxDay(){
		Calendar calt=Calendar.getInstance();
		calt.setTimeInMillis(cal.getTimeInMillis());
		calt.set(Calendar.DATE, 1);
		calt.roll(Calendar.DATE, -1);
		return calt.get(Calendar.DATE);
	}
	
	public String getMinTimeString(){
		return toDateString()+" 00:00:01";
	}
	
	public String getMaxTimeString(){
		return toDateString()+" 23:59:59";
	}
	
	public String toDateString(){
		int year=year(),month=month(),day=day();
		return year+"-"+(month>9?month:"0"+month)+"-"+(day>9?day:"0"+day);
	}

	public String toYearMonthString() {
		int year=year(),month=month();
		return year+"-"+(month>9?month:"0"+month);
	}

	public String toDateNumber(){
		return toDateNumber(year(),month(),day());
	}
	
	public String toShortDateNumber(){
		return toDateNumber(year() % 100,month(),day());
	}
	
	public static String toDateNumber(int year,int month,int day){
		return ""+year+(month>9?month:"0"+month)+(day>9?day:"0"+day);
	}

	public String toDateAndTimeString(){
		return formatDate(cal.getTime(),Option.def_data_format);
	}
	public String format(String format){
		return formatDate(cal.getTime(),format);
	}
	
	public static String formatDate(Date date,String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	public Calendar getCal(){
		return cal;
	}
	
	@Override
	public UCal clone(){
		Calendar calt=Calendar.getInstance();
		calt.setTimeInMillis(cal.getTimeInMillis());
		return new UCal(calt);
	}
	/**
	 * 比较 输入的日期与类的日期的比较
	 * @return 0-相等;1-类的大于输入的;-1-输入的大于类的
	 */
	/**
	 * 比较 输入的日期与类的日期的比较
	 * @param year 年
	 * @param month 月
	 * @param day 日
	 * @return 0-相等;1-类的大于输入的;-1-类的小于输入的
	 */
	public int compareTo(int year,int month,int day){
		if(year>year()){
			return -1;
		}else if(year<year()){
			return 1;
		}else{
			if(month>month()){
				return -1;
			}else if(month<month()){
				return 1;
			}else{
				if(day>day()){
					return -1;
				}else if(day<day()){
					return 1;
				}else{
					return 0;
				}
			}
		}
	}

	@Override
	public int compareTo(UCal o) {
		return this.compareTo(o.cal);
	}
	
	public int compareTo(Calendar o) {
		long lei=this.cal.getTimeInMillis();
		long olei=o.getTimeInMillis();
		if(lei==olei){
			return 0;
		}else if(lei>olei){
			return 1;
		}
		return -1;
	}
	
	@Override
	public String toString() {
		return toDateString();
	}
}
